local que_poxie = fk.CreateSkill {
    name = "que_poxie",
    anim_type = "control",
}
Fk:loadTranslationTable {
    ["que_poxie"] = "破邪",
    [":que_poxie"] = "出牌阶段限一次，你随机获得一张普通锦囊牌的克隆牌；你使用此牌时，你可以令一名其他角色获得一张此牌的复制牌。",

    ["@@que_poxie-turn"] = "破邪",
}
local U = require "packages/utility/utility"
que_poxie:addEffect("active", {
    anim_type = "control",
    prompt = "破邪：是否随机获得一张锦囊牌？",
    card_num = 0,
    target_num = 0,
    can_use = function(self, player)
        return player:usedSkillTimes(que_poxie.name, Player.HistoryPhase) == 0
    end,
    card_filter = Util.FalseFunc,
    on_use = function(self, room, effect)
        local player = effect.from
        local cards = {}
        for _, id in ipairs(Fk:getAllCardIds()) do
            local card = Fk:getCardById(id)
            if card:isCommonTrick() and not card.is_derived then
                table.insertIfNeed(cards, card.name)
            end
        end
        if #cards > 0 then
            local clone = room:printCard(table.random(cards), math.random(1, 4), math.random(1, 13))
            room:obtainCard(player, clone.id, false, fk.ReasonPrey, player, que_poxie.name, "@@que_poxie-turn")
        end
    end,
})

que_poxie:addEffect(fk.CardUsing, {
    can_refresh = function(self, event, target, player, data)
        return
            not (data.extra_data or {}).que_poxieCard and
            table.find(Card:getIdList(data.card), function(id)
                return Fk:getCardById(id):getMark("@@que_poxie-turn") > 0
            end)
    end,
    on_refresh = function(self, event, target, player, data)
        data.extra_data = data.extra_data or {}
        data.extra_data.que_poxieCard = table.filter(Card:getIdList(data.card), function(id)
            return Fk:getCardById(id):getMark("@@que_poxie-turn") > 0
        end)
    end,
})


que_poxie:addEffect(fk.CardUsing, {
    is_delay_effect = true,
    trigger_times = function(self, event, target, player, data)
        return
            type((data.extra_data or {}).que_poxieCard) == "table" and
            #table.filter(Card:getIdList(data.card), function(id)
                return table.contains(data.extra_data.que_poxieCard, id)
            end) or
            0
    end,
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(que_poxie.name) and target == player
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local card = data.card
        local to = room:askToChoosePlayers(player, {
            skill_name = que_poxie.name,
            min_num = 1,
            max_num = 1,
            targets = room:getOtherPlayers(player),
            prompt = "破邪：请选择获得此牌复制牌的角色",
            cancelable = true,
        })[1]
        if to then
        local clone = room:printCard(card.name, card.suit,card.number)
        room:obtainCard(to, clone.id, false, fk.ReasonPrey, player, que_poxie.name)
        end
    end,
})

return que_poxie
